#include "chat_dialog.h"
#include "ui_chat_dialog.h"
#include <QAction>
#include <QRandomGenerator>
#include "chatuserwid.h"
#include <QSize>
#include <QMovie>
#include <QListWidgetItem>
#include <QTimer>
Chat_Dialog::Chat_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Chat_Dialog),_mode(ChatUIMode::ChatMode),
    _state(ChatUIMode::ChatMode),_b_loading(false)
{
    ui->setupUi(this);
    ui->add_friend_btn->setCursor(QCursor(Qt::PointingHandCursor));

//    //设置窗口默认大小
//    ui->chat_user_wid->sizeHint();
    QAction *searchAction = new QAction(ui->search_edit);
    searchAction->setIcon(QIcon(":/images/icon_12m6y6hb65s/sousuo.png"));
    ui->search_edit->addAction(searchAction,QLineEdit::LeadingPosition);
    ui->search_edit->setPlaceholderText(QStringLiteral("搜索"));
    // 创建一个清除动作并设置图标
    QAction *clearAction = new QAction(ui->search_edit);
    clearAction->setIcon(QIcon(""));
    // 初始时不显示清除图标
    // 将清除动作添加到LineEdit的末尾位置
    ui->search_edit->addAction(clearAction, QLineEdit::TrailingPosition);
    // 当需要显示清除图标时，更改为实际的清除图标
    connect(ui->search_edit, &QLineEdit::textChanged, [clearAction](const QString &text) {
        if (!text.isEmpty()) {
            clearAction->setIcon(QIcon(":/images/icon_12m6y6hb65s/qingchu.png"));
        } else {
            clearAction->setIcon(QIcon(" ")); // 文本为空时，切换回透明图标
        }
    });
    // 连接清除动作的触发信号到槽函数，用于清除文本
    connect(clearAction, &QAction::triggered, [this, clearAction]() {
        ui->search_edit->clear();
        clearAction->setIcon(QIcon(" ")); // 清除文本后，切换回透明图标
        ui->search_edit->clearFocus();
        //清除按钮被按下则不显示搜索框
        ShowSearch(false);
    });
    ui->search_edit->SetMaxLength(15);

    //连接加载信号和槽
    connect(ui->chat_user_list, &ChatUserList::sig_loading_chat_user, this,
            &Chat_Dialog::slot_loading_chat_user);

    ShowSearch(false);
    addChatUserList();
}

void Chat_Dialog::ShowSearch(bool bsearch)
{
    if(bsearch){
        ui->chat_user_list->hide();
        ui->con_user_list->hide();
        ui->search_list->show();
        _mode = ChatUIMode::SearchMode;
    }else if(_state == ChatUIMode::ChatMode){
        ui->chat_user_list->show();
        ui->con_user_list->hide();
        ui->search_list->hide();
        _mode = ChatUIMode::ChatMode;
    }else if(_state == ChatUIMode::ContactMode){
        ui->chat_user_list->hide();
        ui->search_list->hide();
        ui->con_user_list->show();
        _mode = ChatUIMode::ContactMode;
    }
}

void Chat_Dialog::addChatUserList()
{
    // 创建QListWidgetItem，并设置自定义的widget
    for(int i = 0; i < 13; i++){
        int randomValue = QRandomGenerator::global()->bounded(100); // 生成0到99之间的随机整数
        int str_i = randomValue%strs.size();
        int head_i = randomValue%heads.size();
        int name_i = randomValue%names.size();
        auto *chat_user_wid = new ChatUserWid();
        chat_user_wid->SetInfo(names[name_i], heads[head_i], strs[str_i]);
        QListWidgetItem *item = new QListWidgetItem;
        //qDebug()<<"chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
        item->setSizeHint(chat_user_wid->sizeHint());
        ui->chat_user_list->addItem(item);
        ui->chat_user_list->setItemWidget(item, chat_user_wid);
    }
}

void Chat_Dialog::slot_loading_chat_user()
{
    if(_b_loading){
        return;
    }
    _b_loading = true;

    QLabel *item_label = new QLabel(this);
    QMovie *movie = new QMovie(":/images/icon_12m6y6hb65s/loading.gif");

    item_label->setMovie(movie);
    item_label->setFixedSize(280,70);
    item_label->setAlignment(Qt::AlignCenter);
    movie->setScaledSize(QSize(50,50));

    QListWidgetItem *item = new QListWidgetItem;
    item->setSizeHint(QSize(280,50));

    ui->chat_user_list->addItem(item);
    ui->chat_user_list->setItemWidget(item,item_label);
    movie->start();

    QTimer::singleShot(200,this,[this,item](){
        qDebug() << "add new data to list.....";
        addChatUserList();
        ui->chat_user_list->takeItem(ui->chat_user_list->row(item));
        ui->chat_user_list->update();
        _b_loading = false;
    });

}

Chat_Dialog::~Chat_Dialog()
{
    delete ui;
}
